package com.lt.flashcard.view;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.util.Clients;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Cart;
import com.lt.flashcard.model.entity.Orderdetail;
import com.lt.flashcard.model.entity.PaymentResult;
import com.lt.flashcard.model.entity.PhoneCardInfo;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.EcommerceService;
import com.lt.flashcard.model.service.Repository;

/**
 * Handle the payment process
 * @author ocsen
 *
 */
public class CartPaymentVm {
	
	private Orderdetail order = null;

	@Init
	public void init() {
		AAService asrv = Repository.getInstance().getAAService();
		if(!asrv.isLogin()) {
			Utility.loginOrAccessDenied(false);
			return;
		}
		
		if(getCart().getItems().size() == 0) {
			Utility.redirect("store.zul", false);
			return;
		}
		
		btnEnable = getCart().getCointotal() <= getUser().getYellowCoin();
	}
	
	/**
	 * Get the card
	 * @return
	 */
	public Cart getCart() {
		EcommerceService srv = Repository.getInstance().getEcommerceService();
		return srv.getCurrentCart();
	}
	
	/**
	 * Payment type
	 */
	private int payType = 0;
	
	public int getPayType() {
		return payType;
	}

	public void setPayType(int payType) {
		this.payType = payType;
	}
	
	/**
	 * Payment button enabled or not
	 */
	private boolean btnEnable = false;
	
	public boolean isBtnEnable() {
		return btnEnable;
	}

	public void setBtnEnable(boolean btnEnable) {
		this.btnEnable = btnEnable;
	}
	
	/**
	 * Process the payment
	 * @throws Exception 
	 */
	@Command
	public void process() {
		PaymentResult result;
		EcommerceService esrv = Repository.getInstance().getEcommerceService();
		switch(payType) {
		case 0: //pay by coin
			if(getCart().getCointotal() <= getUser().getYellowCoin()) {
				result = esrv.processPayment(getUser(), getCart(), Orderdetail.TYPE_COIN, null);
				order = result.getOrder();
				esrv.destroyCart();
				
				if(!result.isSuccess())
					Clients.evalJavaScript("showNotice('" + result.getMessage() + "')");
				
				//redirect
				if(order != null) {
					Utility.rawRedirect("cart_finish.zul?id=" + order.getId(), true);
					return;
				}
			}
			break;
		case 1: //pay by NganLuong
			result = esrv.processPayment(getUser(), getCart(), Orderdetail.TYPE_NGANLUONG, null);
			order = result.getOrder();
			
			if(!result.isSuccess())
				Clients.evalJavaScript("showNotice('" + result.getMessage() + "')");
			
			if(order != null) {
				esrv.destroyCart();
				String url = esrv.generatePaymentUrl(order);
				Utility.rawRedirect(url, true);
			}
			break;
		case 2: //pay by cart
			if(card_code.length() == 0 || card_seri.length() == 0)
				Clients.evalJavaScript("showNotice('', 'Bạn chưa nhập đủ thông tin card')");
			else {
				PhoneCardInfo info = new PhoneCardInfo();
				info.setCard_pin(card_code);
				info.setCard_seri(card_seri);
				if(cardtype == 0)
					info.setType_card(PhoneCardInfo.TYPE_VINAPHONE);
				else if(cardtype == 1)
					info.setType_card(PhoneCardInfo.TYPE_MOBIPHONE);
				else
					info.setType_card(PhoneCardInfo.TYPE_VIETTEL);
				
				if(order == null) {
					result = esrv.processPayment(getUser(), getCart(), Orderdetail.TYPE_CARD, info);
					order = result.getOrder();
				} else {
					result = esrv.updatePayment(getOrder(), info);
					order = result.getOrder();
				}
				
				if(!result.isSuccess())
					Clients.evalJavaScript("showNotice('', '" + result.getMessage() + "')");
				
				if(order != null && order.getStatus() == Orderdetail.STATUS_COMPLETE) {
					esrv.destroyCart();
					Utility.rawRedirect("cart_finish.zul?id=" + order.getId(), true);
					return;
				}
			}
			break;
		}
	}
	
	@NotifyChange("btnEnable")
	@Command
	public void changepaymenttype(@BindingParam("type") int type) {
		payType = type;
		
		switch (payType) {
		case 0: //Pay by yellow coins
			btnEnable = getCart().getCointotal() <= getUser().getYellowCoin();
			break;
		case 1: //Pay by NganLuong
			btnEnable = true;
			break;
		case 2: //Pay by phone cart
			btnEnable = true;
			break;
		default:
			btnEnable = false;
			break;
		}
	}
	
	public User getUser() {//Return current user
		return Utility.getUser();
	}
	
	private int cardtype = 0;
	
	private String card_code = "";
	
	private String card_seri = "";
	
	@Command
	public void changecardtype(@BindingParam("type") int type) {
		setCardtype(type);
	}

	public int getCardtype() {
		return cardtype;
	}

	public void setCardtype(int cardtype) {
		this.cardtype = cardtype;
	}

	public String getCard_code() {
		return card_code;
	}

	public void setCard_code(String card_code) {
		this.card_code = card_code;
	}

	public String getCard_seri() {
		return card_seri;
	}

	public void setCard_seri(String card_seri) {
		this.card_seri = card_seri;
	}

	public Orderdetail getOrder() {
		return order;
	}

	public void setOrder(Orderdetail order) {
		this.order = order;
	}
}
